﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Facade;
using IronOne.SalesOne.BusinessObjects;
using IronOne.SalesOne.DataObjects;


namespace IronOne.SalesOne.SalesOneWeb.Collection
{
    public partial class SearchCollectionVerifiedReport : SOSecurityPage
    {

        StructureFacade facade;
        int userType;
        int userId;
        private static int pageNo = 1;
        UserInformation info;

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                facade = new StructureFacade();

                if (!Page.IsPostBack)
                {
                    txtFrom.Text = DateTime.Today.ToShortDateString();
                    txtTo.Text = DateTime.Today.ToShortDateString();

                    ValidateUserCredentials(Request.Url.LocalPath);

                    User user = facade.GetUser(User.Identity.Name);
                    UserInformation info = facade.GetUserInformation(User.Identity.Name);
                    userType = user.UserType;
                    userId = user.NodeId;

                    StreamWriter file = new StreamWriter(AppDomain.CurrentDomain.BaseDirectory + "\\ZLog.txt", true);
                    file.WriteLine("Name " + User.Identity.Name + " :: userType " + userType + " :: ");
                    file.Close();

                    if (userType == 1)
                    {
                        try
                        {
                            StreamWriter file2 = new StreamWriter(AppDomain.CurrentDomain.BaseDirectory + "\\ZLog.txt", true);
                            file2.WriteLine("usertype 1 and type branch,, dropdown is about to fill");
                            file2.Close();

                            FillDropDown("Branch");
                        }
                        catch (Exception ez)
                        {
                        }
                    }
                    else if (userType == 2)
                    {
                        try
                        {
                            FillDropDown("BranchGrp");
                        }
                        catch (Exception ex)
                        {
                        }
                    }
                    else if (userType == 3)
                    {
                        try
                        {
                            UserInBranch();
                        }
                        catch (Exception ex)
                        {
                        }
                    }
                }
            }catch(Exception ex){}
        }


        public void FillDropDown(string type)
        {
            switch (type)
            {
                case "BranchGrp":

                    //List<Branch> br1 = facade.GetBranchesForGroup(userId);
                    List<Branch> br1 = facade.GetBranchesForGroupV2(userId, new LinqUser().IsLinkedUserCheck(User.Identity.Name));

                    //Branch branch1 = new Branch();
                    //branch1.BranchId = -1;
                    //branch1.BranchName = "Select...";
                    //br1.Insert(0, branch1);
                    //ddlBranch.DataSource = br1;
                    //ddlBranch.DataTextField = "BranchName";
                    //ddlBranch.DataValueField = "BranchId";
                    //ddlBranch.DataBind();

                    //Suren Manawatta
                    //2012-11-05
                    cblBranch.DataSource = br1;
                    cblBranch.DataTextField = "BranchName";
                    cblBranch.DataValueField = "BranchId";
                    cblBranch.DataBind();
                    break;


                case "Branch":
                    //List<Branch> br = facade.GetBranches("BranchName ASC");
                    List<Branch> br = facade.GetBranchesV2("BranchName ASC", new LinqUser().IsLinkedUserCheck(User.Identity.Name));

                    StreamWriter file2 = new StreamWriter(AppDomain.CurrentDomain.BaseDirectory + "\\ZLog.txt", true);
                    file2.WriteLine("displayed list no of branches : " + br.Count + "process finished!!");
                    file2.Close();

                    //Branch branch = new Branch();
                    //branch.BranchId = -1;
                    //branch.BranchName = "Select...";
                    //br.Insert(0, branch);
                    //ddlBranch.DataSource = br;
                    //ddlBranch.DataTextField = "BranchName";
                    //ddlBranch.DataValueField = "BranchId";
                    //ddlBranch.DataBind();

                    //Suren Manawatta
                    //2012-11-05
                    cblBranch.DataSource = br;
                    cblBranch.DataTextField = "BranchName";
                    cblBranch.DataValueField = "BranchId";
                    cblBranch.DataBind();
                    break;

                default:
                    break;
            }
        }


        public void UserInBranch()
        {
            User user = facade.GetUser(User.Identity.Name);
            UserInformation info = facade.GetUserInformation(User.Identity.Name);
            userType = user.UserType;

            FillDropDown("Branch");
            
            //drpBranch.SelectedValue = user.NodeId.ToString();
            
            // Commented By Suren Manawatta
            // 2012-11-06
            //ddlBranch.SelectedValue = info.NodeId.ToString();
            //ddlBranch.Enabled = false;

            cblBranch.SelectedValue = info.NodeId.ToString();
            cblBranch.Enabled = false;
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            try
            {
                FinanceFacade fac = new FinanceFacade();
                StructureFacade sFac = new StructureFacade();

                bool itemSelected = false;
                int itemSelectedCount = 0;
                int[] branchListArray = new int[cblBranch.Items.Count];
                for (int i = 0; i < cblBranch.Items.Count; i++)
                {
                    if (cblBranch.Items[i].Selected)
                    {
                        itemSelectedCount++;
                        itemSelected = true;
                        //break;
                    }
                }

                // Suren Manawatta
                // 2012-11-05
                // Old condition: ddlBranch.SelectedValue == "-1"
                if (!itemSelected)
                {
                    MassageSummery("IIS503");
                }
                else if (itemSelectedCount > 1)
                {
                    MassageSummery("IIS548");
                }
                else if ((Convert.ToDateTime(txtTo.Text).Day - Convert.ToDateTime(txtFrom.Text).Day) > 3)
                {
                    MassageSummery("IIS522");
                }
                else if (Convert.ToDateTime(txtFrom.Text) > DateTime.Today)
                {
                    MassageSummery("IIS521");
                }
                else
                {
                    int inc = 0;
                    string branchListString = "";
                    for (int i = 0; i < cblBranch.Items.Count; i++)
                    {
                        if (cblBranch.Items[i].Selected)
                        {
                            branchListArray[inc++] = int.Parse(cblBranch.Items[i].Value);
                            branchListString += branchListArray[inc-1] + ".";
                        }
                    }

                    // Suren Manawatta
                    // 2012-11-05
                    List<CollectionTransaction> coList = fac.GetCollectionByBranch(branchListArray[0], Convert.ToDateTime(txtFrom.Text), Convert.ToDateTime(txtTo.Text).AddDays(1));


                    //int branchID = sFac.GetBranchByName(ddlBranch.SelectedItem.ToString()).BranchId;
                    //List<CollectionTransaction> coList = fac.GetCollectionByBranch(branchID, Convert.ToDateTime(txtFrom.Text), Convert.ToDateTime(txtTo.Text).AddDays(1));
                    if (coList.Count != 0)
                    {
                        //Response.Redirect("~/Collection/ViewReportCollection.aspx?branchCode=" + branchCode + "&FromDate=" + Convert.ToDateTime(txtFrom.Text) + "&ToDate=" + Convert.ToDateTime(txtTo.Text));

                        //string url = "../Collection/ViewReportCollection.aspx?branchCode=" + branchID + "&FromDate=" + Convert.ToDateTime(txtFrom.Text).ToShortDateString() + "&ToDate=" + Convert.ToDateTime(txtTo.Text).AddDays(1).ToShortDateString();

                        #region MyRegion1
                        string url = "../Collection/ViewReportCollection.aspx?branchCode=" + branchListString + "&FromDate=" + Convert.ToDateTime(txtFrom.Text).ToShortDateString() + "&ToDate=" + Convert.ToDateTime(txtTo.Text).AddDays(1).ToShortDateString();
                        string fullURL = "window.open('" + url + "', '_blank', 'height=700,width=1000,status=yes,toolbar=no,menubar=no,location=no,scrollbars=yes,resizable=no,titlebar=no' );";
                        ScriptManager.RegisterStartupScript(this, typeof(string), "OPEN_WINDOW", fullURL, true); 
                        #endregion

                        #region MyRegion2
                        //string url = "../Collection/ViewReportCollectionAltered.aspx?branchCode=" + branchListString + "&FromDate=" + Convert.ToDateTime(txtFrom.Text).ToShortDateString() + "&ToDate=" + Convert.ToDateTime(txtTo.Text).AddDays(1).ToShortDateString();
                        //string fullURL = "window.open('" + url + "', '_blank', 'height=700,width=1000,status=yes,toolbar=no,menubar=no,location=no,scrollbars=yes,resizable=no,titlebar=no' );";
                        //ScriptManager.RegisterStartupScript(this, typeof(string), "OPEN_WINDOW", fullURL, true); 
                        #endregion
                    }
                    else
                    {
                        MassageSummery("IIS504");
                    }
                }
                // List<CollectionTransaction> colList = fac.GetCollectionByBranch();
            }
            catch (Exception ex)
            {
            }
        }

        protected void btnnewRpt_Click(object sender, EventArgs e)
        {
            try
            {
                FinanceFacade fac = new FinanceFacade();
                StructureFacade sFac = new StructureFacade();

                bool itemSelected = false;
                int[] branchListArray = new int[cblBranch.Items.Count];
                for (int i = 0; i < cblBranch.Items.Count; i++)
                {
                    if (cblBranch.Items[i].Selected)
                    {
                        itemSelected = true;
                        break;
                    }
                }

                // Suren Manawatta
                // 2012-11-05
                // Old condition: ddlBranch.SelectedValue == "-1"
                if (!itemSelected)
                {
                    MassageSummery("IIS503");
                }
                else if ((Convert.ToDateTime(txtTo.Text).Day - Convert.ToDateTime(txtFrom.Text).Day) > 3)
                {
                    MassageSummery("IIS522");
                }
                else if (Convert.ToDateTime(txtFrom.Text) > DateTime.Today)
                {
                    MassageSummery("IIS521");
                }
                else
                {
                    int inc = 0;
                    string branchListString = "";
                    for (int i = 0; i < cblBranch.Items.Count; i++)
                    {
                        if (cblBranch.Items[i].Selected)
                        {
                            branchListArray[inc++] = int.Parse(cblBranch.Items[i].Value);
                            branchListString += branchListArray[inc-1] + ".";
                        }
                    }

                    // Suren Manawatta
                    // 2012-11-05
                    List<CollectionTransaction> coList = fac.GetCollectionByBranchList(branchListArray, Convert.ToDateTime(txtFrom.Text), Convert.ToDateTime(txtTo.Text).AddDays(1));

                    if (coList.Count != 0)
                    {
                        #region MyRegion
                        string url = "../Collection/ViewReportCollectionAltered.aspx?branchCode=" + branchListString + "&FromDate=" + Convert.ToDateTime(txtFrom.Text).ToShortDateString() + "&ToDate=" + Convert.ToDateTime(txtTo.Text).AddDays(1).ToShortDateString();
                        string fullURL = "window.open('" + url + "', '_blank', 'height=700,width=1000,status=yes,toolbar=no,menubar=no,location=no,scrollbars=yes,resizable=no,titlebar=no' );";
                        ScriptManager.RegisterStartupScript(this, typeof(string), "OPEN_WINDOW", fullURL, true); 
                        #endregion
                    }
                    else
                    {
                        MassageSummery("IIS504");
                    }
                }
            }
            catch (Exception ex)
            {
            }
        }
    }
}
